import {request} from '../../utils/http'

Page({
  data: {
    banner: '',
    storeList: [],
    keyword: '',
    loading: false,
    hasSearch:false
  },
  onLoad(query) {
    // 页面加载
    console.info(`Page onLoad with query: ${JSON.stringify(query)}`);
    my.setNavigationBar({
      frontColor:'#ffffff'
    })
    this.setData({
      loading: true
    })
    // this.fetch();
    // this.fetchStoreList();
  },
  onReady() {
    // 页面加载完成
  },
  onShow() {
    // 页面显示
  },
  onHide() {
    // 页面隐藏
  },
  onUnload() {
    // 页面被关闭
  },
  onTitleClick() {
    // 标题被点击
  },
  onPullDownRefresh() {
    // 页面被下拉
  },
  onReachBottom() {
    // 页面被拉到底部
  },
  onShareAppMessage() {
    // 返回自定义分享信息
    return {
      path: 'pages/index/index',
    };
  },
  linkShop(e) {
    const id = e.target.dataset.id
    my.navigateTo({ 
      url: `/pages/store/index?id=${id}&&wechat_sn=${this.data.keyword}` 
    })
  },
  fetch() {
    request('/shop/index', 'get', {
      keyword: this.data.keyword
    }).then(res => {
      my.setStorage({
        key: 'company_name',
        data: res.data.name,
        
      })
      this.setData({
        banner: res.data.banner,
      })
    })
  },
  fetchStoreList() {
    request('/wx/store_search', 'get', {
      wechat_sn:this.data.keyword,
      show_channel:5
    }).then(res => {
      if (res.code == 200) {
        res.data.store['product'] = res.data.product;
        this.getConfig(res.data.store.shop.id)
        this.setData({
          storeList:[res.data.store],
          hasSearch:true
        })
      }
      console.log(res)
    })
    // request('/shop/storeList', 'get', {
    //   keyword: this.data.keyword
    // }).then(res => {
    //   this.setData({
    //     storeList: res.data
    //   })
    //   this.loading = false
    // })
  },
  handleBasicInput(value, e) {
    this.setData({ keyword: value });
  },
  handleBasicClear(value, e) {
    this.setData({ keyword: '' });
    this.fetchStoreList();
  },
  expand(e){
    console.log(e)
    let index = e.currentTarget.dataset.index;
    let item = e.currentTarget.dataset.item;
    let storeList = this.data.storeList;
    storeList[index]['is_expand'] = !storeList[index]['is_expand'];
    this.setData({
      storeList
    })
    // if (storeList[index]['is_expand']) {
    //   request(`/shop/store/${item.id}`,'get').then(res => {
    //     if (res.code === 200) {
    //       storeList[index]['product'] = res.data.product;
    //       this.setData({
    //         storeList
    //       })
    //     }
    //   })
    // } else {
    //   this.setData({
    //     storeList
    //   })
    // }
   
    
  },
  buy(e){
    let {item, store} = e.currentTarget.dataset;
    if (item.price_is_edit == 1) {
      my.navigateTo({ 
        url: `/pages/updateProductPrice/updateProductPrice?id=${item.id}&storeId=${store.id}&wechat_sn=${this.data.keyword}`
      })
    } else {
      my.navigateTo({ 
        url: `/pages/orderConfirm/index?id=${item.id}&storeId=${store.id}&wechat_sn=${this.data.keyword}`
      })
    }
    

  },
  async getConfig(shop_id) {
    let res = await request('/shop/getSwitchConfigs','get', {shop_id: shop_id})
    if (res.code === 200) {
      console.log(res.data)
      // this.globalData.logo = res.data.app_logo
      // this.globalData.mobile_login = res.data.mobile_login

      my.setStorage({
        key:'supplier_id',
        data: res.data.supplier_id
      })

      my.setStorage({
        key: 'mobile_login',
        data: res.data.mobile_login,
        
      })

      my.setStorage({
        key: 'chain_initiative_pay',
        data: res.data.chain_initiative_pay,
        
      })

      my.setStorage({
        key: 'zm_initiative_pay',
        data: res.data.zm_initiative_pay,
        
      })
      

      my.setStorage({
        key: 'card_initiative_pay',
        data: res.data.card_initiative_pay,
        
      })

      my.setStorage({
        key: 'after_sales_hidden',
        data: res.data.after_sales_hidden,
        
      })

      my.setStorage({
        key: 'aiqian_zm',
        data: res.data.aiqian_zm,
        
      })

      my.setStorage({
        key: 'aiqian_card',
        data: res.data.aiqian_card,
        
      })

      my.setStorage({
        key:'kefu_show',
        data: res.data.kefu_show
      })

      my.setStorage({
        key:'card_pay_identity_no_required',
        data: res.data.card_pay_identity_no_required 
      })
      

      my.setStorage({
        key:'appointment_entrance',
        data: res.data.appointment_entrance 
      })
      
    }
  }
});
